package cn.kevinproject.dingtalk_robot.datacenter;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author kevin lau
 */
@Component
public class DataGrabRegistrar implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    private Map<String,DataGrab> dataGrabMap;


    @PostConstruct
    public void init() {
        dataGrabMap = applicationContext.getBeansOfType(DataGrab.class);
    }

    public DataGrab getDataGrab(String key){
        return dataGrabMap.get(key);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void clearCache(){
        dataGrabMap.values().forEach(DataGrab::refreshCache);
    }
}
